
	
// 计算经纬度距离
export const getDistance = (lat1, lon1, lat2, lon2) => {
  const R = 6371e3; // 地球半径，单位米
  const phi1 = lat1 * Math.PI / 180; // 纬度1，弧度制
  const phi2 = lat2 * Math.PI / 180; // 纬度2，弧度制
  const phiDelta = (lat2 - lat1) * Math.PI / 180; // 两点纬度差，弧度制
  const lambdaDelta = (lon2 - lon1) * Math.PI / 180; // 两点经度差，弧度制

  const a = Math.sin(phiDelta / 2) * Math.sin(phiDelta / 2) +
            Math.cos(phi1) * Math.cos(phi2) *
            Math.sin(lambdaDelta / 2) * Math.sin(lambdaDelta / 2);
  const c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));

  const distance = R * c;
  return distance;
}

// 给出一个坐标数组，计算从第一个坐标到最后一个坐标的距离
export const getConstantDistance = (locations) => {
	if(locations.length === 0) return
	let totalDistance = 0
	for(let i = 1;i<locations.length;i++){
		const {pointX:prevPointX,pointY:prevPointY} = locations[i-1]
		const {pointX:nextPointX,pointY:nextPointY} = locations[i]
		totalDistance += getDistance(prevPointX,prevPointY,nextPointX,nextPointY)
	}
	totalDistance = (totalDistance / 1000).toFixed(2)
	return totalDistance
}


